<template>
  <div class="logistics-info">
    <h2>物流相关信息</h2>
    <div class="logistics-info-header">
      <div class="logistics-info-header-left">
        <div class="logistics-status" v-logStatus="orderInfo.logisticsStatus">
          未发货
        </div>
        <div class="logistics-company">{{ orderInfo.logisticsCompany }}</div>
        <div class="logistics-no">{{ orderInfo.logisticsNo }}</div>
      </div>
    </div>
    <ul class="logistics-info-list" v-if="showInfo">
      <li v-for="(v, index) of orderInfo.logisticsInfo" :key="index">
        <div class="logistics-info-list-time">{{ v.time }}</div>
        <div class="logistics-info-list-content">{{ v.content }}</div>
      </li>
    </ul>
    <button v-authority="orderInfo.role" @click="showInfo = true">
      查看物流详情
    </button>
  </div>
</template>

<script>
export default {
  name: "logisticsInfo",
  data() {
    return {
      showInfo: false,
    };
  },
  directives: {
    logStatus: {
      inserted(el, binding) {
        if (binding.value === 0) {
          el.innerHTML = "未发货";
          el.className = "";
          el.classList.add("grey");
        } else if (binding.value === 1) {
          el.innerHTML = "已发货";
          el.classList.add("green");
        } else if (binding.value === 2) {
          el.innerHTML = "已签收";
          el.classList.add("blue");
        } else if (binding.value === 3) {
          el.innerHTML = "未签收";
          el.classList.add("orange");
        } else {
          el.innerHTML = "商品状态未知";
          el.classList.add("red");
        }
      },
    },
    authority: {
      inserted(el, binding) {
        if (
          binding.value !== "admin" &&
          binding.value !== "zhangsan" &&
          binding.value !== "lisi"
        ) {
          el.disabled = true;
        }
      },
    },
  },
  props: {
    orderInfo: {
      type: Object,
      required: true,
    },
  },
};
</script>

<style scoped>
.logistics-info {
  border: 1px solid #ccc;
  border-radius: 5px;
  box-shadow: 0px 0px 10px #eee;
  padding: 10px;
  font-size: 14px;
}

.logistics-info-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.logistics-info-header-left {
  display: flex;
  align-items: center;
}

.logistics-status {
  font-weight: bold;
  font-size: 16px;
}

.blue {
  color: #007bff;
}

.grey {
  color: gray;
}

.green {
  color: green;
}

.orange {
  color: orange;
}

.red {
  color: red;
}

.logistics-company,
.logistics-no {
  margin-left: 10px;
  font-size: 14px;
  color: #333;
}

.logistics-info-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 230px;
  overflow-y: auto;
}

.logistics-info-list li {
  display: flex;
  margin-bottom: 10px;
}

.logistics-info-list-time {
  font-size: 12px;
  color: #999;
  width: 80px;
  text-align: right;
  margin-right: 10px;
}

.logistics-info-list-content {
  font-size: 14px;
  color: #333;
}
</style>
